%% Figure 4 (b) (Require gramm package)
% Adjust the histogram using the household weights.
w1974 = (Wfull(:,1)*52);
w1974(isnan(w1974)) = [];
ev2017 = (U(:,end)*52);
hhw2017 = hhw_vec_L(:,:,end);
hhw2017(isnan(ev2017)) = []; 
hhw2017 = round(hhw2017./10); 
maxhhw = max(hhw2017);
  
ev2017(isnan(ev2017)) = []; 
rc2017 = (W(:,end)*52)./exp(torn(end))';
rc2017(isnan(rc2017)) = []; 
rcvec = NaN(size(rc2017,1),maxhhw);
evvec = NaN(size(rc2017,1),maxhhw);
for i =1:size(ev2017,1)
    rcvec(i,1:hhw2017(i)) = rc2017(i,1);
    evvec(i,1:hhw2017(i)) = ev2017(i,1);
end
rclongnan = reshape(rcvec,[1,size(rc2017,1)*maxhhw]);
evlongnan = reshape(evvec,[1,size(rc2017,1)*maxhhw]);
rclongnan(isnan(rclongnan)) = [];
evlongnan(isnan(evlongnan)) = [];
evrcstuck = [evlongnan;rclongnan];


% Draw a graph: 
namesmat = repmat({'Money metric';'Real consumption'},[1,size(evrcstuck,2)]);
[~,edges] = histcounts(log10(ev2017),22);
g5=gramm('x',evrcstuck ,'color',namesmat);
g5.stat_bin('edges',10.^(edges),'geom','stairs','normalization','probability','fill','transparent'); %Default fill is edges
g5.axe_property('xscale','log','XTickLabel',{'1,000','10,000'},'YTickLabel',{'0','0.02','0.04','0.06','0.08','0.10'}); 
g5.set_names('color','','x','Money metric and real consumption in 2017','y','');
g5.set_text_options('font','Times');
figure('name','Figure 6-2','Position',[100 100 650 400],'NumberTitle','off');
g5.draw();
g5.set_title('FontSize',16)
%g5.export('file_name','fig/MMhist-eps-converted-to','file_type','pdf','units','centimeters');





    function F_X = interpNaN(X,Y)
        indexNaN = isnan(X);
        X(indexNaN) = [];% Eliminate NaN
        Y(indexNaN) = [];% Eliminate NaN
        [X, indexUN] = unique(X); 
        F_X = griddedInterpolant(X,Y(indexUN));
    end

